const webpack = require('webpack')
const path = require('path')
const { TRUE } = require('sass')
function resolve(dir) {
  return path.join(__dirname, './', dir)
}
// const CopyWebpackPlugin = require('copy-webpack-plugin')
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

// const cesiumSource = 'node_modules/mars3d-cesium/Build/Cesium/'

module.exports = {
  publicPath: './',
  assetsDir: './static',
  productionSourceMap: false,
  lintOnSave: true, // 是否开启eslint
  css: {
    loaderOptions: {
      sass: {
        prependData: `
          @import "~@/assets/css/theme.scss";
        `
      }
    }
  },
  devServer: {
    disableHostCheck: true,
    // host: 'localhost', // 也可以直接写IP地址这样方便真机测试
    // // host: '192.168.3.33', // 也可以直接写IP地址这样方便真机测试
    // port: 3002, // 端口号
    // // https: false, // https:{type:Boolean}
    // open: true // 配置自动启动浏览器
    proxy: {
      '^/dwgApi': {
        target: 'http://192.168.3.3:5551', // 周琦然
        // target: 'http://172.30.164.41:5551', // 周琦然-zerotier
        // target: 'http://10.23.10.72:5551', // 中规院内网接口
        // target: 'http://10.1.227.189/future/data', // 政务云接口
        // target:
        //   'http://pad.cdwlkjcmap.com:6080/future-second/dwgData', // 正式线
        changeOrigin: true, // 是否跨域
        ws: true, // proxy websockets
        logLevel: 'debug',
        pathRewrite: {
          // 重写路径
          '^/dwgApi': '' // 代理路径
        }
      },
      '^/api': {
        // target: 'https://c8e2-2407-cdc0-b02f-00-1025.ngrok-free.app/future-city/api', // 周琦然
        // target: 'http://10.23.20.3:5551', // 周琦然
        // target: 'http://10.23.20.7:5551', // 杨静
        // target: 'http://pad.cdwlkjcmap.com:6080/future-second/data', //
        // target: 'http://172.30.164.41:5551', // 周琦然-zerotier
        // target: 'http://172.22.231.151:5551', // 杨静-zerotier
        // target: 'http://10.23.10.72:5551', // 中规院内网接口
        // target: 'http://10.1.227.189/future/data', // 政务云接口
        target:
          'https://pad.cdwlkjcmap.com:6080/future-second/data', // 正式线
        // target:
        //   'http://localhost:6080/future-second/data', // 正式线
        changeOrigin: true, // 是否跨域
        ws: true, // proxy websockets
        logLevel: 'debug',
        pathRewrite: {
          // 重写路径
          '^/api': '' // 代理路径
        }
      },
      '^/iot': {
        target: 'http://pad.cdwlkjcmap.com:6080/iot-api',
        changeOrigin: true,
        ws: true,
        logLevel: 'debug',
        pathRewrite: {
          '^/iot': ''
        }
      }
    }
  },
  chainWebpack(config) {
    config.module.rules.delete('eslint')
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },
  configureWebpack: (config) => {
    let plugins = []
    if (process.env.NODE_ENV === 'production') {
      plugins = [
        new webpack.DefinePlugin({
          CESIUM_BASE_URL: JSON.stringify('static')
        })
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Workers'),
        //     to: 'static/Workers'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Assets'),
        //     to: 'static/Assets'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'ThirdParty'),
        //     to: 'static/ThirdParty'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Widgets'),
        //     to: 'static/Widgets'
        //   }
        // ]),
        // new BundleAnalyzerPlugin()
      ]
    } else {
      plugins = [
        new webpack.DefinePlugin({
          CESIUM_BASE_URL: JSON.stringify('')
        })
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Workers'),
        //     to: 'Workers'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Assets'),
        //     to: 'Assets'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'ThirdParty'),
        //     to: 'ThirdParty'
        //   }
        // ]),
        // new CopyWebpackPlugin([
        //   {
        //     from: path.join(cesiumSource, 'Widgets'),
        //     to: 'Widgets'
        //   }
        // ])
      ]
    }
    return {
      module: {
        unknownContextCritical: false,
        rules: [
          {
            test: /\.js$/,
            enforce: 'pre',
            // include: path.resolve(__dirname, 'node_modules/mars3d-cesium/Source'),
            sideEffects: false,
            use: [
              {
                loader: 'strip-pragma-loader',
                options: {
                  pragmas: {
                    debug: false
                  }
                }
              }
            ]
          }
          // {
          //   test: /\.(png|jpg|gif|svg|cur)$/,
          //   loader: 'file-loader',
          //   options: {
          //     name: '[name].[ext]?[hash]'
          //   }
          // },
          // {
          //   test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
          //   loader: 'file-loader'
          // }
        ]
      },
      optimization: {
        usedExports: true,
        splitChunks: {
          maxInitialRequests: Infinity,
          minSize: 0,
          maxSize: 250000,
          cacheGroups: {
            vendor: {
              chunks: 'initial',
              test: /[\\/]node_modules[\\/]/,
              name: 'vendor',
              minChunks: 1,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 10
            }

            // vendor: {
            //   test: /[\\/]node_modules[\\/]/,
            //   priority: 10,
            //   chunks: 'all',
            //   name(module) {
            //     const packageName = module.context.match(
            //       /[\\/]node_modules[\\/](.*?)([\\/]|$)/
            //     )[1]
            //     return `npm.${packageName.replace('@', '')}`
            //   }
            // }
            // commons: {
            //   name: 'Cesium',
            //   test: /[\\/]node_modules[\\/]mars3d-cesium[\\/]Build[\\/]Cesium/,
            //   priority: 100,
            //   chunks: 'all'
            // }
            // echarts: {
            //   name: 'echarts_vendor',
            //   chunks: 'initial',
            //   priority: 110,
            //   test: function (module) {
            //     const context = module.context;
            //     return context && (context.indexOf('echarts') >= 0 || context.indexOf('zrender') >= 0);
            //   }
            // },
            // runtimeChunk: {
            //   name: 'manifest'
            // }
          }
        },
        minimize: process.env.NODE_ENV === 'production'
      },
      output: {
        sourcePrefix: ' '
      },
      amd: {
        toUrlUndefined: true
      },
      resolve: {
        alias: {
          vue$: 'vue/dist/vue.esm.js',
          '@': path.resolve('src')
        }
      },
      node: {
        fs: 'empty',
        Buffer: false,
        http: 'empty',
        https: 'empty',
        zlib: 'empty'
      },
      plugins: plugins,
      devtool: 'source-map'
    }
  }
}
